package top.linyuxb.lycloud.rpc.samples.client.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.linyuxb.lycloud.common.constant.LyRpcRobinType;
import top.linyuxb.lycloud.rpc.samples.config.service.IClientService;
import top.linyuxb.lycloud.rpc.samples.config.service.IClientService2;
import top.linyuxb.lycloud.rpc.samples.config.service.IClientService3;
import top.linyuxb.lycloud.rpc.samples.config.service.IServerService;
import top.linyuxb.lycloud.rpc.starter.annotation.LyRpcReference;
import top.linyuxb.lycloud.rpc.starter.annotation.LyRpcRibbon;

import java.io.File;

/**
 * @author linyuxb
 * @date 2024/1/13 4:20 下午
 * @desc
 */
@RestController
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);
    @LyRpcReference(version = "002")
    private IClientService clientService;
    @LyRpcReference
    private IClientService2 iClientService2;
    @LyRpcReference(version = "001")
    private IServerService serverService;
    @LyRpcReference
    private IClientService3 iClientService3;

    @GetMapping("/clientService")
    @LyRpcRibbon(fallbackMethod = "defaultClientService")
    public JSONObject clientService() {
        JSONObject json = new JSONObject();
        json.put("name", "张三");
        json.put("school", "惠民小学");
        return clientService.test("锄禾日当午", json);
    }

    public JSONObject defaultClientService(String info, JSONObject json) {
        log.info("defaultClientService:{},{}", info, json);
        JSONObject data = new JSONObject();
        data.put("默认方法", "默认方法");
        return data;
    }

    @GetMapping("/clientService2")
    @LyRpcRibbon(ribbonType = LyRpcRobinType.RANDOM)
    public String clientService2() {
        return iClientService2.getSchool("床前明月光", 25);
    }

    @GetMapping("/serverService")
    @LyRpcRibbon(ribbonType = LyRpcRobinType.IP_HASH)
    public JSONObject serverService() {
        return serverService.test("春种一粒粟", 14, Boolean.TRUE);
    }

    @GetMapping("/pushFile")
    @LyRpcRibbon(ribbonType = LyRpcRobinType.ROUND_ROBIN)
    public String pushFile() {
        File file = FileUtil.file("/Users/fbhou/hfb/project/linyuxb_workspace/ly-cloud/ly-cloud-rpc/ly-cloud-rpc-samples/ly-cloud-rpc-samples-client/src/main/java/top/linyuxb/lycloud/rpc/samples/client/controller/DemoController.java");
        return iClientService3.pushFile(file, 12);
    }

}
